package android.pot.com.smartcar.ui.home;

import android.content.Context;
import android.graphics.Point;
import android.pot.com.smartcar.util.ImageLoadUtils;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by zhaoshilu on 2017/8/29.
 */

public class BGABannerAdapter implements BGABanner.Adapter<ImageView, String> {
    private Context context;

    private boolean autoFit = false;
    //设置图片宽高比
    float scale = (float) 718 / (float) 400;
    int screenWidth;

    private int type = 1;
    public BGABannerAdapter(Context context) {
        this.context = context;
        this.screenWidth = getScreenWidth();
    }
    public BGABannerAdapter(Context context,boolean autoFit) {
        this.context = context;
        this.autoFit = autoFit;
    }
    public BGABannerAdapter(Context context,int type) {
        this.context = context;
        this.screenWidth = getScreenWidth();
        this.type = type;
    }
    private int getScreenWidth(){
        //获取屏幕的宽度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point size = new Point();
        wm.getDefaultDisplay().getSize(size);
        screenWidth = size.x;
        System.out.println("screenWidth:" + screenWidth);
        return screenWidth;
    }
    @Override
    public void fillBannerItem(final BGABanner banner, final ImageView itemView, String model, int position) {
        if (autoFit){
            //计算BGABanner的应有高度
            int viewHeight = Math.round(screenWidth / scale);
            //设置BGABanner的宽高属性
            ViewGroup.LayoutParams banner_params = banner.getLayoutParams();
            banner_params.width = screenWidth;
            banner_params.height = viewHeight;
            banner.setLayoutParams(banner_params);
            //此处使用的是glide的override函数直接设置图片尺寸
            itemView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            GlideLoadUtils.getInstance().glideLoad((Activity) context,model,itemView,R.drawable.zhanweitu_banner);
            if (type == 1) {
                ImageLoadUtils.INSTANCE.loadRoundedImageView(itemView, model);
            }else{
                ImageLoadUtils.INSTANCE.loadImageView(itemView, model);

            }
//            Glide.with(context)
//                    .load(model)
//                    .placeholder(R.drawable.zhanweitu_banner)
//                    .override(banner_params.width,banner_params.height)
//                    .error(R.drawable.zhanweitu_banner)
//                    .centerCrop()
//                    .dontAnimate()
//                    .into(itemView);
        }else{
//            GlideLoadUtils.getInstance().glideLoad((Activity) context,model,itemView,R.drawable.zhanweitu_banner);
            itemView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if (type == 1) {
                ImageLoadUtils.INSTANCE.loadRoundedImageView(itemView, model);
            }else{
                ImageLoadUtils.INSTANCE.loadImageView(itemView, model);

            }
//            Glide.with(context)
//                    .load(model)
//                    .placeholder(R.drawable.zhanweitu_banner)
//                    .error(R.drawable.zhanweitu_banner)
//                    .centerCrop()
//                    .dontAnimate()
//                    .into(itemView);
        }

    }


}

